﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace RJ.Objects.GameEngine
{
    public class Sprite : GameObject
    {
        protected GameObject game;

        protected SpriteSheet spriteSheet;
        public SpriteSheet SpriteSheet
        {
            get { return spriteSheet; }
            set { spriteSheet = value; }
        }

        protected Rectangle rect;
        public Rectangle Rectangle
        {
            get { return rect; }
            set { rect = value; }
        }

        protected RectangleF dest;
        public RectangleF Destination
        {
            get { return dest; }
            set { dest = value; }
        }

        protected PointF velocity;
        public PointF Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public Sprite(GameObject game)
        {
            this.game = game;
        }

        public Sprite(GameObject game, SpriteSheet spriteSheet,
            Rectangle spriteRectangle, RectangleF destination, PointF velocity)
            : this(game)
        {
            this.rect = spriteRectangle;
            this.dest = destination;
            this.velocity = velocity;
            this.spriteSheet = spriteSheet;
        }

        public Sprite(GameObject game, SpriteSheet spriteSheet,
            int rectPosX, int rectPosY, int rectW, int rectH,
            float destPosX, float destPosY, float destW, float destH,
            float velX, float velY)
            : this(
                game, spriteSheet,
                new Rectangle(rectPosX, rectPosY, rectW, rectH),
                new RectangleF(destPosX, destPosY, destW, destH),
                new PointF(velX, velY)) { }

        public override void Update(double gameTime, double elapsedTime)
        {
            dest.X += (velocity.X * (float)elapsedTime);
            dest.Y += (velocity.Y * (float)elapsedTime);
        }

        public override void Draw(Graphics g)
        {
            spriteSheet.DrawSprite(g, this);
        }

        public bool ContainsPoint(Point point)
        {
            return dest.X <= point.X && (dest.X + dest.Width) >= point.X
                && dest.Y <= point.Y && (dest.Y + dest.Height) >= point.Y;
        }
    }
}
